//https://leetcode.cn/problems/longest-turbulent-subarray/submissions/578391656/
class Solution {
public:
    int maxTurbulenceSize(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n);
        auto g = f;
        f[0] = 1; g[0] = 1;
        int max1 = 1, max2 = 1;
        for (int i = 1; i < n; ++i)
        {
            if (nums[i] > nums[i - 1]) { f[i] = g[i - 1] + 1; g[i] = 1; }
            else if (nums[i] == nums[i - 1]) { f[i] = 1; g[i] = 1; }
            else { f[i] = 1; g[i] = f[i - 1] + 1; }
            if (f[i] > max1)max1 = f[i];
            if (g[i] > max2)max2 = g[i];
        }
        return max(max1, max2);
    }
};